class Solution
{
public:
    int LastRemaining_Solution(int n, int m)
    {
        vector<int> children(n);
        for(int i=0; i<n; i++)
        {
            children[i] = i;
        }

        int pos = 0;
        while(children.size()>1)
        {
            pos = (pos+m-1)%children.size();
            children.erase(children.begin()+pos);
        }


        return children[0];
    }
};
